package table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import nodes.Type;
import visitors.SymbolTablePrettyPrintVisitor;

public class DataDeclaration extends Declaration {

    private final List<Type> fields;
    private volatile int hashCode;
    private final SymbolTable scope;

    public DataDeclaration(List<Type> fields, SymbolTable scope)
    {
        if (fields==null || scope == null)
        {
            throw new NullPointerException("Fields cannot be null!");
        }
        ArrayList<Type> temp = new ArrayList<Type>(fields);
        this.fields = Collections.unmodifiableList(temp);
        this.scope = scope;
    }

    public List<Type> getFields()
    {
        return fields;
    }

    public SymbolTable getScope()
    {
        return scope;
    }

    @Override
    public boolean equals(Object aThat)
    {
        if (this == aThat) return true;
        return aThat.getClass() == getClass() &&
                ((DataDeclaration) aThat).fields.equals(fields);
    }

    @Override
    public int hashCode()
    {
        int result = hashCode;
        if (result==0)
        {
            result = 17;
            result = 31 * result + fields.hashCode();
            hashCode = result;
        }
        return result;
    }

    @Override
    public Object accept(SymbolTablePrettyPrintVisitor v)
    {
        return v.visit(this);
    }
}